package com.zg.controller;

import com.zg.service.MqttBrokerAdminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotEmpty;
import java.util.List;


@RestController
@Api(tags = "管理员controller")
@RequestMapping("/broker")
@Validated
public class MqttBrokerAdmin {

    @Resource
    private MqttBrokerAdminService brokerAdmin;

    @ApiOperation(value = "查询所有在线设备")
    @GetMapping("/list_online")
    public List getAllOnline(){
        return brokerAdmin.getOnlineClient();
    }

    @ApiOperation("强制下线某台设备")
    @ApiImplicitParam(type = "query",name = "client_id",value = "设备id",required = true,dataTypeClass = String.class)
    @DeleteMapping("/forced_offline/{client_id}")
    public void OffClient(@PathVariable("client_id")@NotEmpty(message = "客户端id不能为空")String client_id){
        brokerAdmin.delClient(client_id);
    }

    @ApiOperation("取消设备的订阅")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "client_id",value = "设备id",required = true,dataTypeClass = String.class),
            @ApiImplicitParam(name = "list",value = "订阅主题",required = true,dataTypeClass = List.class)
    })
    @DeleteMapping("/unSbu/{client_id}")
    public void unSubTopic(@PathVariable("client_id")@NotEmpty(message = "客户端id不能为空")String client_id,
                           @NotEmpty(message = "主题不能为空不能为空")@RequestParam("list") List<String> list){
        brokerAdmin.delClientTopic(client_id,list);
    }

    @ApiOperation("添加登录设备的账号密码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username",value = "用户名",required = true,dataTypeClass = String.class),
            @ApiImplicitParam(name = "password",value = "密码",required = true,dataTypeClass = String.class)
    })
    @PostMapping("/addUser")
    public Object addUser(@NotEmpty(message = "用户名不能为空")String username,
                          @NotEmpty(message = "密码不能为空")String password){
        return brokerAdmin.addClientPassword(username, password);
    }

    @ApiOperation("删除登录用户")
    @ApiImplicitParam(name = "username",value = "用户名",required = true,dataTypeClass = String.class)
    @DeleteMapping("/delUser")
    public Object delUser(@NotEmpty(message = "用户名不能为空")String username){
        return brokerAdmin.delClientPassword(username);
    }

    @ApiOperation("修改密码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username",value = "用户名",required = true,dataTypeClass = String.class),
            @ApiImplicitParam(name = "password",value = "密码",required = true,dataTypeClass = String.class)
    })
    @PutMapping("/updateUser")
    public Object updateUser(@NotEmpty(message = "用户名不能为空")String username,
                             @NotEmpty(message = "密码不能为空")String password){
        return brokerAdmin.updateClientPassword(username, password);
    }

    @ApiOperation("查询所有设备账号")
    @GetMapping("/allUser")
    public Object allUser(){
        return brokerAdmin.getAllUser();
    }


    @ApiOperation("获取所有的设备订阅主题的信息")
    @GetMapping("/client_topicInfo")
    public Object topicInfo(){
        return brokerAdmin.getAllTopic();
    }

    @ApiOperation("订阅数量的统计和在线设备的统计")
    @GetMapping("/total")
    public Object Total(){
        return brokerAdmin.getTotal();
    }

    @ApiOperation("获取系统的详细信息")
    @GetMapping("/sysInfo")
    public Object SysInfo(){
        return brokerAdmin.getSysInfo();
    }

    @ApiOperation("向指定的主题发送相关的消息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "topic",value = "主题",required = true,dataTypeClass = String.class),
            @ApiImplicitParam(name = "data",value = "发送的消息",required = true,dataTypeClass = String.class)
    })
    @PostMapping("/sendTopicMsg")
    public void SendTopicMsg(@NotEmpty(message = "主题消息不能为空") String topic,
                             @NotEmpty(message = "发送的消息不能为空") String data){
        brokerAdmin.sendTopicMsg(topic,data);
    }


    @ApiOperation("向指定的客户端主题发送消息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "topic",value = "主题",required = true,dataTypeClass = String.class),
            @ApiImplicitParam(name = "data",value = "发送的消息",required = true,dataTypeClass = String.class),
            @ApiImplicitParam(name = "client_id",value = "设备id",required = true,dataTypeClass = String.class),
    })
    @PostMapping("/sendMsgToClient")
    public void SendTopicMsg(@NotEmpty(message = "设备id不能为空") String client_id,
                             @NotEmpty(message = "主题消息不能为空") String topic,
                             @NotEmpty(message = "发送的消息不能为空") String data){
        brokerAdmin.sendMsgToClient(client_id,topic,data);
    }
}
